#include "GamePlayState.h"

#include "SGD Wrappers/CSGD_Direct3D.h"
#include "SGD Wrappers/CSGD_DirectInput.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_XAudio2.h"
#include "MessageSystem.h"
#include "AnimationSystem.h"
#include "EventSystem.h"

// Messages
#include "CreateProjectileMessage.h"
#include "DestroyEntityMessage.h"
#include "CreatePickUpMessage.h"

#include "Emitter.h"
#include "Particle.h"

// Enemies for Testing
#include "BackstabBot.h"
#include "RepairBot.h"
#include "SapperBot.h"
#include "SwarmBot.h"
#include "SwarmBot.h"
#include "TankBot.h"
#include "TurretBot.h"
#include "SecurityCore.h"

#include "OptionsState.h"

// other states
#include "MinigameState.h"
#include "UpgradeState.h"
#include "CutsceneState.h"
#include "OptionsState.h"

// Main menu and game
#include "Game.h"
#include "MainMenuState.h"

// Other Entities
#include "Projectile.h"
#include "PickUp.h"
#include "SpecialProj.h"
#include "Missle.h"

// Tiny xml
#include "tinyxml.h"

#include "Chest.h"
#include "Console.h"

// Include level
#include "Level.h"
#include "Room.h"
#include "Tile.h"

// Need weapon enum types
#include "MeleeWeapon.h"
#include "RangedWeapon.h"

// Level gen
#include "DungeonGenerator.h"

// Level hazards
#include "Hazard.h"

// Toggles the flying debug cam
#define FREECAMERA 0

// Is this the arcade build?
#define ARCADEBUILD 0

GamePlayState::GamePlayState() : PickupSound(-1), CurrentDungeon(0)
{
	// Some test vals
	x = y = 10;
	sound = -1;

	player = nullptr;
	player2 = nullptr;

	twoPlayer = false;

	onShip = true;

	player1Wait = -1;
	player2Wait = -1;

	// Wrapper/Managers
	om = nullptr;
	tm = nullptr;

	curLevel = nullptr;

	// paused false
	paused = false;

	// bitmap font invalid
	FontImageID = -1;

	hudIconsID = -1;
	pausedID   = -1;
	pauseBoxID = -1;

	// Camera
	camX = 0;
	camY = 0;

	pauseY = PAUSE_RESUME;

	// Sound 
	unplugged = -1;
	unplugOnce = false;
	shipMusic = -1;
	researchMusic = -1;
	powerMusic = -1;
	militaryMusic = -1;
	recycleMusic = -1;
	tutorialMusic = -1;
	Enemy_Explode = -1;
	RepairBotMove = -1;
	RepairBotHeal = -1;

	newGame = true;

	// Tutorial
	popUp		= -1;
	buttonA		= -1;
	buttonX		= -1;
	buttonB		= -1;
	buttonLB	= -1;
	buttonRB	= -1;
	leftThumb	= -1;
	buttonBack	= -1;
	buttonY		= -1;

	inPopUp = false;
	curPopUp = 0;


}

GamePlayState::~GamePlayState() 
{ 
	
}

GamePlayState* GamePlayState::GetInstance() 
{
	static GamePlayState instance;
	return &instance;
}

void GamePlayState::Enter()
{
	
	loadingScreen = CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/loadingScreen.png"));

		// Clear the background
	CSGD_Direct3D::GetInstance()->Clear( 0, 0, 0 );

	// Start D3D rendering
	CSGD_Direct3D::GetInstance()->DeviceBegin();
	CSGD_Direct3D::GetInstance()->SpriteBegin();

	CSGD_TextureManager::GetInstance()->Draw(loadingScreen,0,0);

	CSGD_Direct3D::GetInstance()->SpriteEnd();
	CSGD_Direct3D::GetInstance()->DeviceEnd();

	CSGD_Direct3D::GetInstance()->Present();



	MessageSystem::GetInstance()->InitMessageSystem(&MessageProc);

	CSGD_DirectInput::GetInstance()->ClearInput();

	if(CSGD_XAudio2::GetInstance()->MusicIsSongPlaying(Game::GetInstance()->GetMenuMusic()) == true)
		CSGD_XAudio2::GetInstance()->MusicStopSong(Game::GetInstance()->GetMenuMusic());

	// pause to false
	paused = false;

	sound = CSGD_XAudio2::GetInstance()->MusicLoadSong(_T("sound/abandonedutopia.xwm"));

	// Load the pickup sound
	if( PickupSound == -1 )
		PickupSound = CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("sound/randomnoise.wav"));

	if(Enemy_Explode == -1)
		Enemy_Explode = CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("sound/robot_explode.wav"));

	if(RepairBotMove == -1)
		RepairBotMove = CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/repairBotSound.wav"));

	if(RepairBotHeal == -1)
		RepairBotHeal = CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/RepairBot_Heal.wav"));

	// HUD Images
	hudIconsID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/request_1_icons.png"));
	pausedID   = CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/pause_menu.png"));
	pauseBoxID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/pause_menu_box.png"));
	
	om = ObjectManager::GetInstance();
	tm = TileManager::GetInstance();
	em = EmitterManager::GetInstance();

	// Sound
	unplugged = CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/Menu Sounds/unplugged.wav"));


	/*curLevel = new Level;
	curLevel->LoadOptions("graphics/Ship_Options.xml");
	curLevel->LoadRooms("graphics/Ship.xml");

	onShip = true;*/

	ShipLevelStart();

	Animation* tmp = new Animation();
	tmp->LoadAnimationFromFile("RaptorMove.xml");

	int idRaptorMove = AnimationSystem::GetInstance()->AddAnimation(tmp);

	int tmpID[4];
	int tmpIdleID[4];
	int tmpDeathID[4];

	for(int i = 0; i < 4; ++i)
	{
		Animation* tmpAnimation = new Animation();
		tmpAnimation->LoadAnimationFromFile("player_walk.xml", i);
		tmpID[i] = AnimationSystem::GetInstance()->AddAnimation(tmpAnimation);
	}

	for(int i = 0; i < 4; ++i)
	{
		Animation* tmpAnimation = new Animation();
		tmpAnimation->LoadAnimationFromFile("player_idle.xml", i);
		tmpIdleID[i] = AnimationSystem::GetInstance()->AddAnimation(tmpAnimation);
	}

	for(int i = 0; i < 4; ++i)
	{
		Animation* tmpAnimation = new Animation();
		tmpAnimation->LoadAnimationFromFile("player_death.xml", i);
		tmpDeathID[i] = AnimationSystem::GetInstance()->AddAnimation(tmpAnimation);
	}

	if(player == nullptr)
	{
		player = new Player();
		player->SetPlayerNum(1);
		player->SetX(300);
		player->SetY(300);
		player->SetMoveAnimation(tmpID);
		player->SetIdleAnimation(tmpIdleID);
		player->SetDeathAnimation(tmpDeathID);
		om->AddObject(player);
		player->AddRef();
		int purchased[10] = {};
		player->SetEquipment(0, 0, -1, -1, purchased);
	}

	if(twoPlayer == true && player2 == nullptr)
	{
		player2 = new Player();
		player2->SetPlayerNum(2);
		player2->SetX(400);
		player2->SetY(300);
		player2->SetMoveAnimation(tmpID);
		player2->SetIdleAnimation(tmpIdleID);
		player2->SetDeathAnimation(tmpDeathID);
		om->AddObject(player2);
		player2->AddRef();

		player1Wait = -1;
		player2Wait = -1;
	}

	// Getting bitmap font
	GroupFont =	CBitmapFont::GetInstance();

	// Load bitmap font
	FontImageID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Robovox.bmp"), D3DCOLOR_XRGB(0,0,0));

	// Set the bitmap font's values
	GroupFont->SetValues(32, 32, 16, 14, ' ', FontImageID);

	//e = Emitter::LoadEmitter("Emitters/test.xml");


	// Cursor position
	cursorY = 150;
	cursorX = 640;

	// Create enemies in level
	/*std::vector<Room*> LevelsRooms = l.GetRoomVec();
	for(int i = 0; i < l.GetRoomCount(); ++i)
	LevelsRooms[i]->AddEnemy();*/

	inMinigame = false;
	inUpgradeState = false;
	inCutscene = false;
	inOptions = false;

	// Ready the cutscene state
	CutsceneState::GetInstance()->Enter();

	// Ready the minigame
	MinigameState::GetInstance()->Enter();

	// Ready the Options State
	//OptionsState::GetInstance()->Enter();

	// Run the starting cutscene
	if(newGame == true)
	{
		CutsceneState::GetInstance()->RunCutscene(CutsceneState::SCENE_BEGINNING);
		inCutscene = true;
	}

	pauseY = PAUSE_RESUME;

	unplugOnce = false;

	shipMusic		= CSGD_XAudio2::GetInstance()->MusicLoadSong(_T("Sound/Music/shipMusic.xwm"));
	researchMusic	= CSGD_XAudio2::GetInstance()->MusicLoadSong(_T("Sound/Music/researchMusic.xwm"));
	powerMusic		= CSGD_XAudio2::GetInstance()->MusicLoadSong(_T("Sound/Music/powerMusic.xwm"));
	militaryMusic	= CSGD_XAudio2::GetInstance()->MusicLoadSong(_T("Sound/Music/militaryMusic.xwm"));
	recycleMusic	= CSGD_XAudio2::GetInstance()->MusicLoadSong(_T("Sound/Music/recycleMusic.xwm"));
	tutorialMusic	= CSGD_XAudio2::GetInstance()->MusicLoadSong(_T("Sound/Music/tutorialMusic.xwm"));

	popUp		= CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/tutorial_popup.png"), D3DCOLOR_XRGB(0,0,0));
	buttonA		= CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/ButtonA.png"), D3DCOLOR_XRGB(0,0,0));
	buttonX		= CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/ButtonX.png"), D3DCOLOR_XRGB(0,0,0));
	buttonB		= CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/ButtonB.png"), D3DCOLOR_XRGB(0,0,0));
	buttonLB	= CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/LeftShoulder.png"), D3DCOLOR_XRGB(0,0,0));
	buttonRB	= CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/RightShoulder.png"), D3DCOLOR_XRGB(0,0,0));
	leftThumb	= CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/LeftThumbstick.png"), D3DCOLOR_XRGB(0,0,0));
	buttonBack  = CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/ButtonBack.png"), D3DCOLOR_XRGB(0,0,0));
	buttonY		= CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/ButtonY.png"), D3DCOLOR_XRGB(0,0,0));

	inPopUp = false;
	curPopUp = 0;

	for(int i = 0; i < 8; i++)
		hasPopped[i] = false;
}

void GamePlayState::Exit()
{
	MinigameState::GetInstance()->Exit();
	UpgradeState::GetInstance()->Exit();
	CutsceneState::GetInstance()->Exit();
	OptionsState::GetInstance()->Exit();

	EmitterManager::GetInstance()->Shutdown();

	Game::GetInstance()->SaveGame(saveSlot);

	// Unload sound
	if( PickupSound != -1 )
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(PickupSound);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(PickupSound);
		PickupSound = -1;
	}
	// Unload sound
	if( unplugged != -1 )
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(unplugged);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(unplugged);
		unplugged = -1;
	}
	if( shipMusic != -1 )
	{
		CSGD_XAudio2::GetInstance()->MusicStopSong(shipMusic);
		CSGD_XAudio2::GetInstance()->MusicUnloadSong(shipMusic);
		shipMusic = -1;
	}
	if( researchMusic != -1 )
	{
		CSGD_XAudio2::GetInstance()->MusicStopSong(researchMusic);
		CSGD_XAudio2::GetInstance()->MusicUnloadSong(researchMusic);
		researchMusic = -1;
	}
	if( powerMusic != -1 )
	{
		CSGD_XAudio2::GetInstance()->MusicStopSong(powerMusic);
		CSGD_XAudio2::GetInstance()->MusicUnloadSong(powerMusic);
		powerMusic = -1;
	}
	if( militaryMusic != -1 )
	{
		CSGD_XAudio2::GetInstance()->MusicStopSong(militaryMusic);
		CSGD_XAudio2::GetInstance()->MusicUnloadSong(militaryMusic);
		militaryMusic = -1;
	}
	if( recycleMusic != -1 )
	{
		CSGD_XAudio2::GetInstance()->MusicStopSong(recycleMusic);
		CSGD_XAudio2::GetInstance()->MusicUnloadSong(recycleMusic);
		recycleMusic = -1;
	}
	if( recycleMusic != -1 )
	{
		CSGD_XAudio2::GetInstance()->MusicStopSong(recycleMusic);
		CSGD_XAudio2::GetInstance()->MusicUnloadSong(recycleMusic);
		recycleMusic = -1;
	}
	if( tutorialMusic != -1 )
	{
		CSGD_XAudio2::GetInstance()->MusicStopSong(tutorialMusic);
		CSGD_XAudio2::GetInstance()->MusicUnloadSong(tutorialMusic);
		tutorialMusic = -1;
	}
	if( Enemy_Explode != -1 )
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(Enemy_Explode);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(Enemy_Explode);
		Enemy_Explode = -1;
	}
	if( RepairBotMove != -1 )
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(RepairBotMove);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(RepairBotMove);
		RepairBotMove = -1;
	}
	if( RepairBotHeal != -1 )
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(RepairBotHeal);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(RepairBotHeal);
		RepairBotHeal = -1;
	}

	// Unload bitmap font
	if(FontImageID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(FontImageID);
		FontImageID = -1;
	}



	if(hudIconsID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(hudIconsID);
		hudIconsID = -1;
	}
	if(pausedID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(pausedID);
		pausedID = -1;
	}
	if(pauseBoxID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(pauseBoxID);
		pauseBoxID = -1;
	}


	if(popUp != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(popUp);
		popUp = -1;
	}

	if(buttonA != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(buttonA);
		buttonA = -1;
	}
	if(buttonX != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(buttonX);
		buttonX = -1;
	}
	if(buttonB != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(buttonB);
		buttonB = -1;
	}
	if(buttonLB != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(buttonLB);
		buttonLB = -1;
	}
	if(buttonRB != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(buttonRB);
		buttonRB = -1;
	}
	if(leftThumb != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(leftThumb);
		leftThumb = -1;
	}
	if(buttonBack != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(buttonBack);
		buttonBack = -1;
	}
	if(buttonY != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(buttonY);
		buttonY = -1;
	}

	if(loadingScreen != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(loadingScreen);
		loadingScreen = -1;
	}




	if( sound != -1 )
	{
		if( CSGD_XAudio2::GetInstance()->MusicIsSongPlaying(sound) )
			CSGD_XAudio2::GetInstance()->MusicStopSong(sound);

		CSGD_XAudio2::GetInstance()->MusicUnloadSong(sound);

		sound = -1;
	}

	if(curLevel != nullptr)
	{
		delete curLevel;
		curLevel = nullptr;
	}

	// Release all the objects in the Object Manager
	if(om != nullptr )
	{
		om->RemoveAllObjects();
		om = nullptr;
	}

	if( tm != nullptr )
	{
		tm->RemoveAllTiles();
		tm = nullptr;
	}

	onShip = false;

	if(testconsole != nullptr)
	{
		testconsole->Release();
		testconsole = nullptr;
	}

	// Cleanup the chests

	chestList.clear();


	// Cleanup the level hazards
	/*if(LevelHazards.size() > 0)
	{
	for(int i = 0; i < (int)LevelHazards.size(); ++i)
	{
	LevelHazards[i]->Release();
	}*/
	LevelHazards.clear();
	//}

	Doors.clear();

	// Delete Player
	if(player != nullptr)
	{
		player->Release();
		player = nullptr;
	}

	if(player2 != nullptr)
	{
		player2->Release();
		player2 = nullptr;
	}

	AnimationSystem::GetInstance()->Shutdown();

	//for(int i = 0; i < (int)enemies.size(); i++)
	//{
	//delete enemies[i];
	//enemies.erase(enemies.begin() + i);
	//}

	// pause to false
	paused = false;

	MessageSystem::GetInstance()->ShutdownMessageSystem();
	EventSystem::GetInstance()->ShutdownEventSystem();


	//e->Shutdown();
}

bool GamePlayState::Input()
{
	// Get Direct Input
	CSGD_DirectInput* di = CSGD_DirectInput::GetInstance();

#if FREECAMERA
	if( di->KeyDown(DIK_UP) )
		camY -= 10;
	if( di->KeyDown(DIK_DOWN) )
		camY += 10;
	if( di->KeyDown(DIK_LEFT) )
		camX -= 10;
	if( di->KeyDown(DIK_RIGHT) )
		camX += 10;
#endif

	if(newGame == true)
	{
		if(inPopUp == true)
		{
			if(di->JoystickButtonPressed(3))
			{
				//if(curPopUp == 0)
				//{
				//	hasPopped[0] = true;
				//	curPopUp = 1;
				//}
				//else
				{
					inPopUp = false;
					hasPopped[curPopUp] = true;

					if(curPopUp == 7)
					{
						if(fromMain == true)
						{
							if(CSGD_XAudio2::GetInstance()->MusicIsSongPlaying(tutorialMusic) == true)
								CSGD_XAudio2::GetInstance()->MusicStopSong(tutorialMusic);
							Game::GetInstance()->ChangeState(MainMenuState::GetInstance());

							return true;
						}
						else if(fromMain == false)
						{
							newGame = false;
							if(CSGD_XAudio2::GetInstance()->MusicIsSongPlaying(tutorialMusic) == true)
								CSGD_XAudio2::GetInstance()->MusicStopSong(tutorialMusic);
							ShipLevelStart();

							return true;
						}
					}
					else
						curPopUp += 1;
				}

				di->ClearInput();
				return true;
			}
		}

		//oldX = (int)player->GetX();
	}


	if( !inMinigame && !inUpgradeState && !inCutscene && !inOptions)
	{
		if(newGame == true && di->JoystickButtonPressed(6) && fromMain == false && paused == false)
		{
			newGame = false;
			//ShipLevelStart();
			camX = 0;
			camY = 0;
			GamePlayState::GetInstance()->SetMuliplayer(twoPlayer);
			GamePlayState::GetInstance()->SetSaveSlot(saveSlot);
			GamePlayState::GetInstance()->SetNewGame(false);////////////////
			Game::GetInstance()->ChangeState(GamePlayState::GetInstance());
			return true;
		}
		else if(newGame == true && di->JoystickButtonPressed(6) && fromMain == true && paused == false)
		{
			//newGame = false;
			// Bug Fix #39 Start - Brandon Jones
			fromMain = false;
			// Bug Fix #39 End
			Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
			return true;
		}

#if ARCADEBUILD
		if( di->MouseButtonPressed(0) && inPopUp == false)
#else
		if((di->KeyPressed(DIK_ESCAPE) || di->JoystickButtonPressed(7) || di->JoystickIsUnplugged() == true)  && inPopUp == false)
#endif
		{
			paused = true;

			if(di->JoystickIsUnplugged(0) == true && unplugOnce == false)
			{
				CSGD_XAudio2::GetInstance()->SFXPlaySound(unplugged);
				unplugOnce = true;
			}

			pausePlayerNum = 0;
		}
		else
			unplugOnce = false;

		if(twoPlayer == true)
		{
			if(di->JoystickButtonPressed(7,1) || di->JoystickIsUnplugged(1) == true)
			{
				paused = true;

				if(di->JoystickIsUnplugged(1) == true && unplugOnce == false)
				{
					CSGD_XAudio2::GetInstance()->SFXPlaySound(unplugged);
					unplugOnce = true;		
				}

				pausePlayerNum = 1;
			}
			else
				unplugOnce = false;

			if(newGame == true && di->JoystickButtonPressed(6,1))
			{
				newGame = false;
				ShipLevelStart();
			}
			else if(newGame == true && di->JoystickButtonPressed(6,1) && fromMain == true)
			{
				newGame = false;
				Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
			}
		}

		if(paused == true)
		{
			Game* game = Game::GetInstance();

			if(di->JoystickButtonPressed(1,pausePlayerNum) || di->JoystickButtonPressed(6,pausePlayerNum))
			{
				paused = false;
				CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuBack());
				return true;
			}

			if(di->JoystickButtonPressed(0,pausePlayerNum))
			{
				if(pauseY == PAUSE_RESUME)
				{
					paused = false;
					CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuSelect());
				}
				else if(pauseY == PAUSE_OPTIONS)
				{
					inOptions = true;			
					CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuSelect());
					OptionsState::GetInstance()->Enter();
				}
				else if(pauseY == PAUSE_OUIT)
				{
					CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuSelect());
					Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
					return true;
				}
			}

			if(di->JoystickGetLStickDirPressed(DIR_UP,pausePlayerNum))
			{
				switch(pauseY)
				{
				case PAUSE_RESUME:
					CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuMove());
					pauseY = PAUSE_OUIT;
					break;

				case PAUSE_OPTIONS:
					CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuMove());
					pauseY = PAUSE_RESUME;
					break;

				case PAUSE_OUIT:
					CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuMove());
					pauseY = PAUSE_OPTIONS;
					break;
				}
			}

			if(di->JoystickGetLStickDirPressed(DIR_DOWN,pausePlayerNum))
			{
				switch(pauseY)
				{
				case PAUSE_RESUME:
					CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuMove());
					pauseY = PAUSE_OPTIONS;
					break;

				case PAUSE_OPTIONS:
					CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuMove());
					pauseY = PAUSE_OUIT;
					break;

				case PAUSE_OUIT:
					CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuMove());
					pauseY = PAUSE_RESUME;
					break;
				}
			}
		}
	}

	if(inMinigame == true)
		return MinigameState::GetInstance()->Input();
	else if(inUpgradeState == true)
		return UpgradeState::GetInstance()->Input();
	else if( inCutscene )
		return CutsceneState::GetInstance()->Input();
	else if(inOptions)
		return OptionsState::GetInstance()->Input();

	return true;

}

void GamePlayState::Update(float elapsed)
{
	if(onShip == true)
	{
		if(CSGD_XAudio2::GetInstance()->MusicIsSongPlaying(shipMusic) == false)
		{
			CSGD_XAudio2::GetInstance()->SilenceAll();
			CSGD_XAudio2::GetInstance()->MusicPlaySong(shipMusic,true);

			/*CSGD_XAudio2::GetInstance()->MusicStopSong(researchMusic);
			CSGD_XAudio2::GetInstance()->MusicStopSong(powerMusic);
			CSGD_XAudio2::GetInstance()->MusicStopSong(militaryMusic);
			CSGD_XAudio2::GetInstance()->MusicStopSong(recycleMusic);*/
		}

	}

	//////////////////////////////////////////////
	// Tutorial PopUps ///////////////////////////
	//////////////////////////////////////////////

	if(newGame == true)
	{
		if(oldX != player->GetX())
			after0 = true;

		/* Controls */
		if(onShip == true && hasPopped[0] == false)
			inPopUp = true;

		/* Upgrade Station */
		if(onShip == true && inUpgradeState == true && hasPopped[1] == false)
			inPopUp = true;

		/* Clear Room */
		if(player->GetRoomNum() == 1 && hasPopped[2] == false)
			inPopUp = true;

		/* Circuitry/Metal */
		if(player->GetRoomNum() == 1 && curLevel->GetRoom(player->GetRoomNum())->GetEnemyVec().size() == 0 && hasPopped[3] == false)
			inPopUp = true;

		/* Level Hazards */
		if(player->GetRoomNum() == 2 && hasPopped[4] == false)
			inPopUp = true;

		/* Chest Minigame */
		if(player->GetRoomNum() == 2 && curLevel->GetRoom(player->GetRoomNum())->GetEnemyVec().size() == 0 && hasPopped[5] == false)
			inPopUp = true;

		/* SAVIOR Pieces */
		if(onShip == false && player->GetRoomNum() == curLevel->GetRoomCount()-2 && curLevel->GetRoom(player->GetRoomNum())->GetEnemyVec().size() == 0 && hasPopped[6] == false)
			inPopUp = true;

		/* Complete */
		if(player->GetRoomNum() == curLevel->GetRoomCount()-1)
			inPopUp = true;
	}

	/////////////////////////////////////////////
	/////////////////////////////////////////////


	if(!paused || inOptions )
	{
		if( !inMinigame && !inUpgradeState && !inCutscene && !inOptions)
		{
			// See if the game is over
			if( GetPlayer()->GetHealth() <= 0 )
			{
				// Bug Fix #26 Start - Brandon Jones
				newGame = false;
				// Bug Fix #26 End

				// Bug Fix #49 Start - Brandon Jones
				CSGD_XAudio2::GetInstance()->SilenceAll();
				//Bug Fix #49 End

				CutsceneState::GetInstance()->RunCutscene(CutsceneState::SCENE_GAMEOVER);
				inCutscene = true;
				return;
			}
			if( GetPlayer2() )
			{
				if( GetPlayer2()->GetHealth() <= 0 )
				{
					// Bug Fix #26 Start - Brandon Jones
					newGame = false;
					// Bug Fix #26 End

					// Bug Fix #49 Start - Brandon Jones
					CSGD_XAudio2::GetInstance()->SilenceAll();
					//Bug Fix #49 End

					CutsceneState::GetInstance()->RunCutscene(CutsceneState::SCENE_GAMEOVER);
					inCutscene = true;
					return;
				}
			}

			if(inPopUp == false)
			{
				MessageSystem::GetInstance()->ProcessMessages();
				EventSystem::GetInstance()->ProcessEvents();
				om->UpdateAll(elapsed);
				om->CheckCollisions();
				em->Update(elapsed);
				//e->Update(elapsed);
			}

		}
		else if(inMinigame == true)
		{	// Run the minigame
			MinigameState::GetInstance()->Update(elapsed);
			if( MinigameState::GetInstance()->Won || MinigameState::GetInstance()->Failed )
				inMinigame = false;
		}
		else if(inUpgradeState == true)
		{	// Run the upgrade State
			UpgradeState::GetInstance()->Update(elapsed);
			if( UpgradeState::GetInstance()->GetExit() == true)
				inUpgradeState = false;
		}
		else if( inCutscene )
		{	// Run the current cutscene
			CutsceneState::GetInstance()->Update(elapsed);
			if( CutsceneState::GetInstance()->Finished )
				inCutscene = false;
		}
		else if(inOptions)
		{	// Run the options menu
			OptionsState::GetInstance()->Update(elapsed);
			if(OptionsState::GetInstance()->GetExit() == true)
			{
				inOptions = false;
				OptionsState::GetInstance()->SaveOptions("OptionsData.xml");
				OptionsState::GetInstance()->SetExit(false);
			}
		}

	}

	if(player != nullptr)
	{
		// Check to see if the camera has updated
		int oldcamx = camX;
		int oldcamy = camY;
		if(twoPlayer == false)
		{
#if !FREECAMERA
			if(camX >= (int)player->GetX() + (int)player->GetWidth() )
				camX -= Game::GetInstance()->GetScreenWidth();
			if(camX <= (int)player->GetX() - Game::GetInstance()->GetScreenWidth() )
				camX += Game::GetInstance()->GetScreenWidth();
			if(camY >= (int)player->GetY() + (int)player->GetHeight() )
				camY -= Game::GetInstance()->GetScreenHeight();
#endif
			if(camY <= (int)player->GetY() - Game::GetInstance()->GetScreenHeight() )
			{
#if !FREECAMERA
				if(onShip == false)
					camY += Game::GetInstance()->GetScreenHeight();
				else
#endif
				{
					if( onShip )
						DoMagic(MOREMAGIC);
				}
			}
		}
		else if(player2 != nullptr)
		{
			if((int)player->GetX() + (int)player->GetWidth() > camX + Game::GetInstance()->GetScreenWidth())
			{
				player1Wait = 1;
				player->SetX((float)(camX + Game::GetInstance()->GetScreenWidth()) - player->GetWidth());
			}
			else if((int)player->GetX() < camX)
			{
				player1Wait = 2;
				player->SetX((float)camX);
			}
			else if((int)player->GetY() + (int)player->GetHeight() > camY + Game::GetInstance()->GetScreenHeight())
			{
				player1Wait = 3;
				player->SetY((float)(camY + Game::GetInstance()->GetScreenHeight()) - player->GetHeight());
			}
			else if( (int)player->GetY() < camY)
			{
				player1Wait = 4;
				player->SetY((float)camY);
			}
			else
			{
				player1Wait = 0;
			}

			if((int)player2->GetX() + (int)player2->GetWidth() > camX + Game::GetInstance()->GetScreenWidth())
			{
				player2Wait = 1;
				player2->SetX((float)(camX + Game::GetInstance()->GetScreenWidth()) - player2->GetWidth());
			}
			else if((int)player2->GetX() < camX)
			{
				player2Wait = 2;
				player2->SetX((float)camX);
			}
			else if((int)player2->GetY() + (int)player2->GetHeight() > camY + Game::GetInstance()->GetScreenHeight())
			{
				player2Wait = 3;
				player2->SetY((float)(camY + Game::GetInstance()->GetScreenHeight()) - player2->GetHeight());
			}
			else if((int)player2->GetY() < camY)
			{
				player2Wait = 4;
				player2->SetY((float)camY);
			}
			else
			{
				player2Wait = 0;
			}


			if(player2Wait == player1Wait)
			{
				switch(player2Wait)
				{
				case 0:
					break;
				case 1:
					camX += Game::GetInstance()->GetScreenWidth();
					player->SetX(player->GetX() + player->GetWidth()*2);
					player2->SetX(player2->GetX() + player2->GetWidth()*2);
					player1Wait = 0;
					player2Wait = 0;
					break;
				case 2:
					camX -= Game::GetInstance()->GetScreenWidth();
					player->SetX(player->GetX() - player->GetWidth()*2);
					player2->SetX(player2->GetX() - player2->GetWidth()*2);
					player1Wait = 0;
					player2Wait = 0;
					break;
				case 3:
					if(onShip == false)
					{
						camY += Game::GetInstance()->GetScreenHeight();
						player->SetY(player->GetY() + player->GetHeight()*2);
						player2->SetY(player2->GetY() + player2->GetHeight()*2);
						player1Wait = 0;
						player2Wait = 0;
					}
					else
					{
						if( testconsole )
						{
							testconsole->Release();
							testconsole = nullptr;
						}

						DoMagic(MOREMAGIC);
					}
					break;
				case 4:
					camY -= Game::GetInstance()->GetScreenHeight();
					player->SetY(player->GetY() - player->GetHeight()*2);
					player2->SetY(player2->GetY() - player2->GetHeight()*2);
					player1Wait = 0;
					player2Wait = 0;
					break;
				}
			}
		}

		// Update the player's room number if the camera has moved
		if( (oldcamx != camX || oldcamy != camY) && !onShip )
		{
			// Get the location of the room in the array from Dungeon
			int playerRoomX = (int)player->GetX()/800;
			int playerRoomY = (int)player->GetY()/600;
			Room* currentRoom = Dungeon::GetInstance()->rooms[playerRoomX][playerRoomY];
			player->SetRoomNum(currentRoom->GetRoomNumber());
		}

	}
}

void GamePlayState::Render()
{

	// Selected and not selected colors
	DWORD isSelected = D3DCOLOR_ARGB(255,255,0,0);
	DWORD notSelected =  D3DCOLOR_ARGB(255,0,255,255);

	//e->Render();

	HUDmagic();

	// Render tiles before objects
	tm->RenderTiles();
	om->RenderAll();
	em->Render();

	//GroupFont->Print("Loading...",100,450,2.0f,notSelected);

	if(newGame == true)
	{
		CSGD_TextureManager::GetInstance()->Draw(buttonBack,672,440,.5f,.5f);
		if(Game::GetInstance()->GetLanguage() == true)
			GroupFont->Print("Skip", 672,480,.5f,isSelected);
		else
			GroupFont->Print("Overslaan", 620,480,.5f,isSelected);

		if(inPopUp == true)
		{
			// Bug Fix #2 Start - Brandon Jones
			if(CSGD_XAudio2::GetInstance()->SFXIsSoundPlaying(player->GetWalkSound()))
				CSGD_XAudio2::GetInstance()->SFXStopSound(player->GetWalkSound());
			if(twoPlayer && CSGD_XAudio2::GetInstance()->SFXIsSoundPlaying(player2->GetWalkSound()))
				CSGD_XAudio2::GetInstance()->SFXStopSound(player2->GetWalkSound());
			// Bug Fix #2 End

			switch(curPopUp)
			{
				case 0:		// Controls - ship - right away
				{
					CSGD_TextureManager::GetInstance()->Draw(popUp,0,0);

					CSGD_TextureManager::GetInstance()->Draw(buttonA,  267,207,.5f,.5f);
					CSGD_TextureManager::GetInstance()->Draw(buttonX,  267,240,.5f,.5f);
					CSGD_TextureManager::GetInstance()->Draw(buttonB,  267,273,.5f,.5f);
					CSGD_TextureManager::GetInstance()->Draw(buttonLB, 267,311,.35f,.35f);
					CSGD_TextureManager::GetInstance()->Draw(buttonRB, 317,311,.35f,.35f);
					CSGD_TextureManager::GetInstance()->Draw(leftThumb,267,339,.5f,.5f);

					if(Game::GetInstance()->GetLanguage() == true)
					{
						GroupFont->Print("- Attack",		310,217,.5f,notSelected);
						GroupFont->Print("- Special Attack",310,250,.5f,notSelected);
						GroupFont->Print("- Switch Weapons",310,283,.5f,notSelected);
						GroupFont->Print("- Use Skills",	375,316,.5f,notSelected);
						GroupFont->Print("- Move",			310,349,.5f,notSelected);
					}
					else
					{
						GroupFont->Print("- Aanvallen",			310,217,.5f,notSelected);
						GroupFont->Print("- Speciale Aanval",	310,250,.5f,notSelected);
						GroupFont->Print("- Schakelaar Wapens",	310,283,.5f,notSelected);
						GroupFont->Print("- Bekwaamheid",		375,316,.5f,notSelected);
						GroupFont->Print("- Bewegen",			310,349,.5f,notSelected);
					}
				}
				break;

				case 1:		// UpgradeState - ship - after controls
				{
					CSGD_TextureManager::GetInstance()->Draw(popUp,0,0);
					if(Game::GetInstance()->GetLanguage() == true)
						GroupFont->Print("The console inside the\nship is the Upgrade\nStation. You can buy\nnew weapons and skills\nhere. This is also\nwhere you can change \nloadouts.",277,217,.5f,notSelected);
					else
						GroupFont->Print("De console in het schip\nis de Upgrade Station.\nU kunt hier kopen\nvan nieuwe wapens en\nvaardigheden. Dit is\nook waar u kunt\nloadouts.",277,217,.5f,notSelected);

				}
				break;

				case 2:		// Clear room - first room in dungeon - right away
				{
					CSGD_TextureManager::GetInstance()->Draw(popUp,0,0);
					if(Game::GetInstance()->GetLanguage() == true)
						GroupFont->Print("Every room has a laser\ndoor leading to the\nnext room, which you\ncannot pass until all\nenemies in the room\nare destroyed.",277,217,.5f,notSelected);
					else
						GroupFont->Print("Elke kamer heeft een\nlaser deur naar de\nvolgende kamer, die u\nniet door totdat alle\nvijanden in de kamer\nworden vernietigd.",277,217,.5f,notSelected);
				}
				break;

				case 3:		// Circuitry/Metal - first room in dungeon - after defeating all enemies
				{
					CSGD_TextureManager::GetInstance()->Draw(popUp,0,0);
					if(Game::GetInstance()->GetLanguage() == true)
						GroupFont->Print("After enemies are\ndestroyed, they leave\nbehind circuitry and\nscrap metal. These\nitems are used to\npurchase weapons and\nskills.",277,217,.5f,notSelected);
					else
						GroupFont->Print("Na vijanden worden\nvernietigd, laten ze\nachter circuits en\nschroot. Deze items\nworden gebruikt om\nwapens en vaardigheden\naan te schaffen.",277,217,.5f,notSelected);

				}
				break;

				case 4:		// Level Hazards - second room of dungeon - right away
				{
					CSGD_TextureManager::GetInstance()->Draw(popUp,0,0);
					if(Game::GetInstance()->GetLanguage() == true)
						GroupFont->Print("Some rooms have level\nhazards that can hurt\nyou or enemies, watch\nout.",277,217,.5f,notSelected);
					else
						GroupFont->Print("Sommige kamers hebben\neen niveau van gevaren\ndie kunnen kwetsen of\nvijanden, kijk uit.",277,217,.5f,notSelected);

				}
				break;

				case 5:		// Loot Chests - second to last room of dungeon - all enemies gone
				{
					CSGD_TextureManager::GetInstance()->Draw(popUp,0,0);
					if(Game::GetInstance()->GetLanguage() == true)
						GroupFont->Print("Rooms can also have\nloot chests, which can\ngive you more circuitry\nand scrap metal. Upon\nopening chests, a\nminigame will start in\nwhich you must avoid\nenemy ships.",277,217,.5f,notSelected);
					else
						GroupFont->Print("De kamers kunnen ook\nbuit kisten, die kan\nje meer circuits en\nschroot. Bij het openen\nvan kisten, zal een\nminigame te starten\nwaarin je moet\nvijandelijke schepen te\nvoorkomen.",277,217,.5f,notSelected);

				}
				break;

				case 6:		// SAVIOR Pieces - last room of dungeon - right away
				{
					CSGD_TextureManager::GetInstance()->Draw(popUp,0,0);
					if(Game::GetInstance()->GetLanguage() == true)
						GroupFont->Print("After defeating the\nfinal boss of a dungeon,\nyou can collect the\nSAVIOR piece and return\nback to your ship.",277,217,.5f,notSelected);
					else
						GroupFont->Print("Na het verslaan van\nde eindbaas van een\nkerker, kun je\nverzamelen de\nVerlosser stuk en weer\nterug naar je schip.",277,217,.5f,notSelected);

				}
				break;

				case 7:		// Complete - last room of dungeon - right away
				{
					CSGD_TextureManager::GetInstance()->Draw(popUp,0,0);
					if(Game::GetInstance()->GetLanguage() == true)
						GroupFont->Print("You have completed the\ntutorial. Now you must\nsave Earth.",277,217,.5f,notSelected);
					else
						GroupFont->Print("U bent klaar met de\ntutorial. Nu moet je\nbespaart Aarde.",277,217,.5f,notSelected);

				}
				break;
			}

			CSGD_TextureManager::GetInstance()->Draw(buttonY,435,345,.4f,.4f);
			if(Game::GetInstance()->GetLanguage() == true)
				GroupFont->Print("- Done",470,355,.4f,isSelected);
			else
				GroupFont->Print("- Klaar",470,355,.4f,isSelected);
		}
	}

	// Draw pause menu
	if(paused == true)
	{
		if(Game::GetInstance()->GetLanguage() == true)
		{
			CSGD_TextureManager::GetInstance()->Draw(pausedID,0,0);
			CSGD_TextureManager::GetInstance()->Draw(pauseBoxID,XBOX,pauseY);
		}
		else
		{
			//{XBOX = 272, PAUSE_RESUME = 204, PAUSE_OPTIONS = 259, PAUSE_OUIT = 309};

			Game::GetInstance()->DrawBitmapFontTCHAR(pauseStr,	 75, 50,				2.5f, D3DCOLOR_ARGB(255,0,255,255));
			Game::GetInstance()->DrawBitmapFontTCHAR(resumeStr,	 XBOX, PAUSE_RESUME,	pauseY==PAUSE_RESUME ? 1.5f : 1.0f,		pauseY==PAUSE_RESUME ? isSelected : notSelected);
			Game::GetInstance()->DrawBitmapFontTCHAR(optionStr,	 XBOX, PAUSE_OPTIONS,	pauseY==PAUSE_OPTIONS ? 1.5f : 1.0f,	pauseY==PAUSE_OPTIONS ? isSelected : notSelected);
			Game::GetInstance()->DrawBitmapFontTCHAR(quitStr,	 XBOX, PAUSE_OUIT,		pauseY==PAUSE_OUIT ? 1.5f : 1.0f,		pauseY==PAUSE_OUIT ? isSelected : notSelected);
		}
	}

	/*if(twoPlayer)
	{
		RECT tmp;
		tmp.left = LONG(player->GetX() + 20);
		tmp.top = LONG(player->GetY() - 10);
		tmp.right = LONG(player->GetX() + 30);
		tmp.bottom = LONG(player->GetY());

		Game::GetInstance()->DrawBitmapFontTCHAR(L"P1", (int)player->GetX(), (int)player->GetY(), 0.35f,
			D3DCOLOR_ARGB(255,0,255,255));

		RECT tmp2;
		tmp2.left = LONG(player2->GetX() + 20);
		tmp2.top = LONG(player2->GetY() - 10);
		tmp2.right = LONG(player2->GetX() + 30);
		tmp2.bottom = LONG(player2->GetY());

		CSGD_Direct3D::GetInstance()->GetSprite()->Flush();
		CSGD_Direct3D::GetInstance()->DrawRect(tmp, 255, 0, 0);
		CSGD_Direct3D::GetInstance()->DrawRect(tmp2, 0, 0, 255);
	}*/

	if( inMinigame )
		MinigameState::GetInstance()->Render();

	if(inUpgradeState == true)
		UpgradeState::GetInstance()->Render();

	if( inCutscene )
		CutsceneState::GetInstance()->Render();

	if(inOptions)
		OptionsState::GetInstance()->Render();
}

void GamePlayState::MessageProc(Message* msg)	
{
	GamePlayState* self = GamePlayState::GetInstance();

	switch(msg->GetMessageID())
	{
	case MSG_CREATE_PROJECTILE:
		{
			Entity* owner = dynamic_cast<CreateProjectileMessage*>(msg)->GetOwner();

			if(owner != nullptr)
			{
				switch(dynamic_cast<CreateProjectileMessage*>(msg)->GetBulletType())
				{
				case BASIC:
					{
						Projectile* proj = new Projectile();

						switch(owner->GetDirection())
						{
						case 0:
							proj->SetX(owner->GetX() + 15);
							proj->SetY(owner->GetY() + 3);
							break;

						case 1:
							proj->SetX(owner->GetX() - 30);
							proj->SetY(owner->GetY() + 30);
							break;

						case 2:
							proj->SetX(owner->GetX() - 5);
							proj->SetY(owner->GetY() + 5);
							break;

						case 3:
							proj->SetX(owner->GetX());
							proj->SetY(owner->GetY() - 25);
							break;
						}


						proj->SetBulletType(dynamic_cast<CreateProjectileMessage*>(msg)->GetBulletType());
						proj->SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Dummy Images/laserShotPlayer.png")));
						proj->SetWidth(8);
						proj->SetHeight(8);
						proj->SetMoveSpeed(500.0f);
						proj->SetDamage(20.0f);
						proj->SetTrail(Emitter::LoadEmitter("Emitters/laserShot.xml"));
						proj->GetTrail()->Loop(true);
						proj->SetOwner(owner);
						proj->SetDirection(owner->GetDirection());

						self->om->AddObject(proj);
						proj->Release();
					}
					break;

				case ROCKET:
					{
						Projectile* proj = new Projectile();

						//int x = (int)owner->GetX();
						//int y = (int)owner->GetY();

						switch(owner->GetDirection())
						{
						case 0:
							proj->SetX(owner->GetX());
							proj->SetY(owner->GetY());
							break;

						case 1:
							proj->SetX(owner->GetX() - 40);
							proj->SetY(owner->GetY() + 30);
							break;

						case 2:
							proj->SetX(owner->GetX() - 22);
							proj->SetY(owner->GetY() + 5);
							break;

						case 3:
							proj->SetX(owner->GetX() - 20);
							proj->SetY(owner->GetY() - 30);
							break;
						}

						proj->SetBulletType(RW_ROCKET);
						proj->SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/rocket.png")));
						proj->SetWidth(16);
						proj->SetHeight(8);
						proj->SetMoveSpeed(500.0f);
						proj->SetDamage(70.0f);
						proj->SetTrail(Emitter::LoadEmitter("Emitters/rocketFollow.xml"));
						proj->GetTrail()->Loop(true);
						proj->SetOwner(owner);
						proj->SetDirection(owner->GetDirection());

						self->om->AddObject(proj);
						proj->Release();
					}
					break;

				case MISSLE_BARRAGE:
					{
						if(self->GetPlayer()->GetRoomNum() >= 0)
						{
							vector<Enemy*> enemyList = self->curLevel->GetRoom(self->GetPlayer()->GetRoomNum())->GetEnemyVec();

							float tmpX = 0;
							float tmpY = 0;

							switch(owner->GetDirection())
							{
							case 0:
								{
									tmpX = (owner->GetX());
									tmpY = (owner->GetY());
								}
								break;

							case 1:
								{
									tmpX = (owner->GetX() - 40);
									tmpY = (owner->GetY() + 30);
								}
								break;

							case 2:
								{
									tmpX = (owner->GetX() - 22);
									tmpY = (owner->GetY() + 5);
								}
								break;

							case 3:
								{
									tmpX = (owner->GetX() - 20);
									tmpY = (owner->GetY() - 30);
								}
								break;
							}

							if(enemyList.size() > 0)
							{
								for(int i = 0; i < 8; ++i)
								{
									Missle* proj = new Missle(enemyList[i % enemyList.size()]);
									proj->SetBulletType(MISSLE_BARRAGE);
									proj->SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/rocket.png")));
									proj->SetWidth(16);
									proj->SetHeight(8);
									proj->SetMoveSpeed(100.0f);
									proj->SetDamage(30.0f);
									proj->SetOwner(owner);
									proj->SetDirection(i%4);
									proj->SetX(tmpX);
									proj->SetY(tmpY);
									proj->SetDelayID(i);
									self->om->AddObject(proj);
									proj->Release();
								}
							}
						}
						//else
						//{
						//	for(int i = 0; i < 8; ++i)
						//		{
						//			Missle* proj = new Missle(nullptr);
						//			proj->SetX(owner->GetX() + 32);
						//			proj->SetY(owner->GetY() + 16);
						//			proj->SetBulletType(MISSLE_BARRAGE);
						//			proj->SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Dummy Images/rocket.png")));
						//			proj->SetWidth(32);
						//			proj->SetHeight(32);
						//			proj->SetMoveSpeed(100.0f);
						//			proj->SetDamage(30.0f);
						//			proj->SetOwner(nullptr);
						//			proj->SetDelayID(i);
						//			proj->SetDirection(i % 4);//owner->GetDirection());
						//			self->om->AddObject(proj);
						//			proj->Release();
						//		}
						//}
					}
					break;

				case EMP:
					{
						Projectile* proj = new Projectile();

						switch(owner->GetDirection())
						{
						case 0:
							proj->SetX(owner->GetX() + 15);
							proj->SetY(owner->GetY() - 3);
							break;

						case 1:
							proj->SetX(owner->GetX() - 30);
							proj->SetY(owner->GetY() + 30);
							break;

						case 2:
							proj->SetX(owner->GetX() - 5);
							proj->SetY(owner->GetY() + 5);
							break;

						case 3:
							proj->SetX(owner->GetX());
							proj->SetY(owner->GetY() - 30);
							break;
						}


						proj->SetBulletType(RW_EMP);
						proj->SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/empShot.png")));
						proj->SetWidth(16);
						proj->SetHeight(16);
						proj->SetMoveSpeed(500.0f);
						proj->SetDamage(30.0f);
						proj->SetTrail(Emitter::LoadEmitter("Emitters/empShot.xml"));
						proj->GetTrail()->Loop(true);
						proj->SetOwner(owner);
						proj->SetDirection(owner->GetDirection());

						self->om->AddObject(proj);
						proj->Release();
					}
					break;

				case EMP_BURST:
					{
						SpecialProj* proj = new SpecialProj(EMP_BURST);

						proj->SetX(owner->GetX());
						proj->SetY(owner->GetY());
						proj->SetBulletType(EMP_BURST);
						proj->SetWidth(32);
						proj->SetHeight(32);
						proj->SetDamage(30.0f);
						proj->SetOwner(owner);
						proj->SetDirection(owner->GetDirection());

						self->om->AddObject(proj);
						proj->Release();
					}
					break;

				case FLAME:
					{
						Projectile* proj = new Projectile();

						int x = (int)owner->GetX();
						int y = (int)owner->GetY();

						switch(owner->GetDirection())
						{
						case 0:
							proj->SetX(owner->GetX());
							proj->SetY(owner->GetY());
							break;

						case 1:
							proj->SetX(owner->GetX() - 40);
							proj->SetY(owner->GetY() + 10);
							break;

						case 2:
							proj->SetX(owner->GetX() - 30);
							proj->SetY(owner->GetY());
							break;

						case 3:
							proj->SetX(owner->GetX() - 25);
							proj->SetY(owner->GetY() - 30);
							break;
						}

						proj->SetBulletType(RW_FLAME);
						proj->SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Dummy Images/burn.png")));
						proj->SetWidth(32);
						proj->SetHeight(32);
						proj->SetMoveSpeed(250.0f);
						proj->SetDamage(5.0f);
						proj->SetOwner(owner);
						proj->SetDirection(owner->GetDirection());

						self->om->AddObject(proj);
						proj->Release();
					}
					break;

				case NAPALM:
					{
						for(int i = 0; i < 20; ++i)
						{
							SpecialProj* proj = new SpecialProj(NAPALM);
							proj->SetX(owner->GetX());
							proj->SetY(owner->GetY());
							proj->SetBulletType(NAPALM);
							proj->SetWidth(32);
							proj->SetHeight(32);
							proj->SetMoveSpeed(i * 20.0f);
							proj->SetDamage(1.0f);
							proj->SetOwner(owner);
							proj->SetDirection(owner->GetDirection());
							self->om->AddObject(proj);
							proj->Release();
						}
					}
					break;

				case ENEMY:
					{
						Projectile* proj = new Projectile();

						proj->SetX(owner->GetRect().left+(owner->GetWidth()/2.0f));
						proj->SetY(owner->GetRect().top+(owner->GetHeight()/2.0f));
						proj->SetBulletType(ENEMY);
						proj->SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Dummy Images/laserShot.png")));
						proj->SetWidth(16);
						proj->SetHeight(8);
						proj->SetMoveSpeed(600.0f);
						proj->SetDamage(5.0f);
						proj->SetDirection(owner->GetDirection());
						proj->SetOwner(owner);

						self->om->AddObject(proj);
						proj->Release();
					}
					break;

				case SAWBOT:
					{
						Projectile* proj = new Projectile();
						proj->SetX(owner->GetX());
						proj->SetY(owner->GetY());
						proj->SetBulletType(ENEMY);
						proj->SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Dummy Images/saw.png")));
						proj->SetWidth(32);
						proj->SetHeight(32);
						proj->SetMoveSpeed(400.0f);
						proj->SetDamage(20.0f);
						proj->SetDirection(owner->GetDirection());
						proj->SetOwner(owner);
						self->om->AddObject(proj);
						proj->Release();

						proj = new Projectile();
						proj->SetX(owner->GetX() + owner->GetWidth()/4);
						proj->SetY(owner->GetY());
						proj->SetBulletType(ENEMY);
						proj->SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Dummy Images/saw.png")));
						proj->SetWidth(32);
						proj->SetHeight(32);
						proj->SetMoveSpeed(200.0f);
						proj->SetDamage(25.0f);
						proj->SetDirection(owner->GetDirection());
						proj->SetOwner(owner);
						self->om->AddObject(proj);
						proj->Release();

						proj = new Projectile();
						proj->SetX(owner->GetX() + owner->GetWidth()/2);
						proj->SetY(owner->GetY());
						proj->SetBulletType(ENEMY);
						proj->SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Dummy Images/saw.png")));
						proj->SetWidth(32);
						proj->SetHeight(32);
						proj->SetMoveSpeed(400.0f);
						proj->SetDamage(20.0f);
						proj->SetDirection(owner->GetDirection());
						proj->SetOwner(owner);
						self->om->AddObject(proj);
						proj->Release();

						proj = new Projectile();
						proj->SetX(owner->GetX() + 3 * owner->GetWidth()/4);
						proj->SetY(owner->GetY());
						proj->SetBulletType(ENEMY);
						proj->SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Dummy Images/saw.png")));
						proj->SetWidth(32);
						proj->SetHeight(32);
						proj->SetMoveSpeed(200.0f);
						proj->SetDamage(25.0f);
						proj->SetDirection(owner->GetDirection());
						proj->SetOwner(owner);
						self->om->AddObject(proj);
						proj->Release();

						proj = new Projectile();
						proj->SetX(owner->GetX() + owner->GetWidth());
						proj->SetY(owner->GetY());
						proj->SetBulletType(ENEMY);
						proj->SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Dummy Images/saw.png")));
						proj->SetWidth(32);
						proj->SetHeight(32);
						proj->SetMoveSpeed(400.0f);
						proj->SetDamage(20.0f);
						proj->SetDirection(owner->GetDirection());
						proj->SetOwner(owner);
						self->om->AddObject(proj);
						proj->Release();
					}
					break;

				case BASIC_CLOCKED:
					{
						Projectile* proj = new Projectile();

						proj->SetX(owner->GetX());
						proj->SetY(owner->GetY());
						proj->SetBulletType(dynamic_cast<CreateProjectileMessage*>(msg)->GetBulletType());
						proj->SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Dummy Images/laserShotPlayer.png")));
						proj->SetWidth(8);
						proj->SetHeight(8);
						proj->SetMoveSpeed(500.0f);
						proj->SetDamage(30.0f); // TEST VARIABLE, CHANGE LATER
						proj->SetOwner(owner);
						proj->SetDirection(owner->GetDirection());

						self->om->AddObject(proj);
						proj->Release();
					}
					break;

				case ROCKET_CLOCKED:
					{
						Projectile* proj = new Projectile();

						proj->SetX(owner->GetX() + 32);
						proj->SetY(owner->GetY() + 16);
						proj->SetBulletType(RW_ROCKET);
						proj->SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/rocket.png")));
						proj->SetWidth(32);
						proj->SetHeight(32);
						proj->SetMoveSpeed(500.0f);
						proj->SetDamage(105.0f);
						proj->SetTrail(Emitter::LoadEmitter("Emitters/rocketFollow.xml"));
						proj->GetTrail()->Loop(true);
						proj->SetOwner(owner);
						proj->SetDirection(owner->GetDirection());

						self->om->AddObject(proj);
						proj->Release();
					}
					break;

				case EMP_CLOCKED:
					{
						Projectile* proj = new Projectile();

						proj->SetX(owner->GetX());
						proj->SetY(owner->GetY());
						proj->SetBulletType(RW_EMP);
						proj->SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/empShot.png")));
						proj->SetWidth(32);
						proj->SetHeight(32);
						proj->SetMoveSpeed(500.0f);
						proj->SetDamage(45.0f);
						proj->SetTrail(Emitter::LoadEmitter("Emitters/empShot.xml"));
						proj->GetTrail()->Loop(true);
						proj->SetOwner(owner);
						proj->SetDirection(owner->GetDirection());

						self->om->AddObject(proj);
						proj->Release();
					}
					break;

				case FLAME_CLOCKED:
					{
						Projectile* proj = new Projectile();

						proj->SetX(owner->GetX());
						proj->SetY(owner->GetY());
						proj->SetBulletType(RW_FLAME);
						proj->SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Dummy Images/burn.png")));
						proj->SetWidth(32);
						proj->SetHeight(32);
						proj->SetMoveSpeed(250.0f);
						proj->SetDamage(7.5f);
						proj->SetOwner(owner);
						proj->SetDirection(owner->GetDirection());

						self->om->AddObject(proj);
						proj->Release();
					}
					break;
				}
			}
		}
		break;

	case MSG_DESTROY_ENTITY:
		{
			Entity* entity = dynamic_cast<DestroyEntityMessage*>(msg)->GetEntity();

			if(entity->GetType() == OBJ_ENEMY)
				CSGD_XAudio2::GetInstance()->SFXPlaySound(self->Enemy_Explode);

			// If the object is an image tile, remove it from the tilemanager
			if( entity->GetType() == OBJ_TILE )
			{
				if( ((Tile*)(entity))->GetTileType() == TILE_BACKGROUND )
					self->tm->RemoveTile((Tile*)entity);
				else
					self->om->RemoveObject(entity);
			}
			else // Remove the object from the Object Manager
				self->om->RemoveObject(entity);

		}
		break;

	case MSG_CREATE_PICKUP:
		{
			tVector2D pos = dynamic_cast<CreatePickUpMessage*>(msg)->GetPosition();
			int objType = dynamic_cast<CreatePickUpMessage*>(msg)->GetObjectType();

			// Offset the pickups so they're accessible from the chest
			int xoff = 0;
			int yoff = 0;

			if(objType == OBJ_CHEST )
				yoff = 32;

			switch(dynamic_cast<CreatePickUpMessage*>(msg)->GetItemType())
			{

			case METAL:
				{
					PickUp* pickup = new PickUp();

					pickup->SetX(pos.fX + xoff);
					pickup->SetY(pos.fY + yoff);
					pickup->SetItemType(dynamic_cast<CreatePickUpMessage*>(msg)->GetItemType());
					pickup->SetValue(dynamic_cast<CreatePickUpMessage*>(msg)->GetValue());
					pickup->SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Dummy Images/scrapMetal.png")));
					pickup->SetWidth(32);
					pickup->SetHeight(32);

					self->om->AddObject(pickup);
					pickup->Release();
				}
				break;

			case CIRCUITRY:
				{
					PickUp* pickup = new PickUp();

					pickup->SetX(pos.fX+10+xoff);
					pickup->SetY(pos.fY+yoff);
					pickup->SetItemType(dynamic_cast<CreatePickUpMessage*>(msg)->GetItemType());
					pickup->SetValue(dynamic_cast<CreatePickUpMessage*>(msg)->GetValue());
					pickup->SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Dummy Images/scrapCircuitry.png")));
					pickup->SetWidth(32);
					pickup->SetHeight(32);

					self->om->AddObject(pickup);
					pickup->Release();
				}
				break;

			}

		}
		break;
	}
}

void GamePlayState::ShipLevelStart()
{
	// Clear the background
	CSGD_Direct3D::GetInstance()->Clear( 0, 0, 0 );

	// Start D3D rendering
	CSGD_Direct3D::GetInstance()->DeviceBegin();
	CSGD_Direct3D::GetInstance()->SpriteBegin();

	CSGD_TextureManager::GetInstance()->Draw(loadingScreen,0,0);

	CSGD_Direct3D::GetInstance()->SpriteEnd();
	CSGD_Direct3D::GetInstance()->DeviceEnd();

	CSGD_Direct3D::GetInstance()->Present();



	EventSystem::GetInstance()->ClearEvents();
	MessageSystem::GetInstance()->ClearMessages();

	EmitterManager::GetInstance()->Shutdown();

	if(curLevel != nullptr)
	{
		delete curLevel;

		player->SetHealth(player->GetMaxHealth());
		// BUG #51 Start - Ben Feldmann ////////////////
		player->SetEnergy(player->GetMaxEnergy());
		// BUG #51 End ////////////////////////////////

		if(player2 != nullptr)
		{
			player2->SetHealth(player2->GetMaxHealth());
			// BUG #51 Start - Ben Feldmann ////////////////
			player2->SetEnergy(player2->GetMaxEnergy());
			// BUG #51 End ////////////////////////////////
		}

		if(chestList.size() > 0)
		{
			for(int i = 0; i < (int)chestList.size(); ++i)
			{
				DestroyEntityMessage* msg = new DestroyEntityMessage(chestList[i]);
				MessageSystem::GetInstance()->SendMsg(msg);
				msg = nullptr;
			}
			chestList.clear();
		}

		if(LevelHazards.size() > 0)
		{
			for(int i = 0; i < (int)LevelHazards.size(); ++i)
			{
				DestroyEntityMessage* msg = new DestroyEntityMessage(LevelHazards[i]);
				MessageSystem::GetInstance()->SendMsg(msg);
				msg = nullptr;
			}
			LevelHazards.clear();
		}

		if( Doors.size() >  0)
		{
			for(unsigned int i = 0; i < Doors.size(); ++i )
			{
				DestroyEntityMessage* msg = new DestroyEntityMessage(Doors[i]);
				MessageSystem::GetInstance()->SendMsg(msg);
				msg = nullptr;
			}
			Doors.clear();
		}

		em->Shutdown();
	}

	curLevel = new Level();
	curLevel->LoadOptions("graphics/Ship_Options.xml");
	curLevel->LoadRooms("graphics/Ship.xml");
	onShip = true;

	testconsole = new Console();
	testconsole->SetX(220);
	testconsole->SetY(220);
	testconsole->SetHeight(32);
	testconsole->SetWidth(32);
	om->AddObject(testconsole);

	if(player != nullptr)
	{
		player->SetX(300);
		player->SetY(300);
	}
	if(twoPlayer == true && player2 != nullptr)
	{
		// Bug Fix # Start - Brandon Jones
		camX = 0;
		camY = 0;
		// Bug Fix # End

		player2->SetX(400);
		player2->SetY(300);

		player1Wait = 0;
		player2Wait = 0;
	}
}

void GamePlayState::DoMagic(Magic amount)
{
		// Clear the background
	CSGD_Direct3D::GetInstance()->Clear( 0, 0, 0 );

	// Start D3D rendering
	CSGD_Direct3D::GetInstance()->DeviceBegin();
	CSGD_Direct3D::GetInstance()->SpriteBegin();

	CSGD_TextureManager::GetInstance()->Draw(loadingScreen,0,0);

	CSGD_Direct3D::GetInstance()->SpriteEnd();
	CSGD_Direct3D::GetInstance()->DeviceEnd();

	CSGD_Direct3D::GetInstance()->Present();

	if( amount == MAGIC )
		return; // Not enough magic

	EventSystem::GetInstance()->ClearEvents();
	MessageSystem::GetInstance()->ClearMessages();

	EmitterManager::GetInstance()->Shutdown();

	if(chestList.size() > 0)
	{
		for(int i = 0; i < (int)chestList.size(); ++i)
		{
			DestroyEntityMessage* msg = new DestroyEntityMessage(chestList[i]);
			MessageSystem::GetInstance()->SendMsg(msg);
			msg = nullptr;
		}
		chestList.clear();
	}

	if(LevelHazards.size() > 0)
	{
		for(int i = 0; i < (int)LevelHazards.size(); ++i)
		{
			DestroyEntityMessage* msg = new DestroyEntityMessage(LevelHazards[i]);
			MessageSystem::GetInstance()->SendMsg(msg);
			msg = nullptr;
		}
		LevelHazards.clear();
	}

	if( Doors.size() >  0)
	{
		for(unsigned int i = 0; i < Doors.size(); ++i )
		{
			DestroyEntityMessage* msg = new DestroyEntityMessage(Doors[i]);
			MessageSystem::GetInstance()->SendMsg(msg);
			msg = nullptr;
		}
		Doors.clear();
	}

	if( curLevel != nullptr )
	{
		delete curLevel;

		for(int i = 0; i < (int)chestList.size(); ++i)
		{
			DestroyEntityMessage* msg = new DestroyEntityMessage(chestList[i]);
			MessageSystem::GetInstance()->SendMsg(msg);
			msg = nullptr;
		}

		chestList.clear();
	}

	if( testconsole )
	{
		DestroyEntityMessage* msg = new DestroyEntityMessage(testconsole);
		MessageSystem::GetInstance()->SendMsg(msg);
		testconsole->Release();
		testconsole = nullptr;
	}

	if(newGame == false)
		curLevel = Dungeon::GetInstance()->GenerateLevel(Dungeon::DUNGEON(CurrentDungeon));
	else if(newGame == true)
		curLevel = Dungeon::GetInstance()->GenerateTutorial();

	onShip = false;

	if(newGame == false)
	{
		inCutscene = true;
		CSGD_XAudio2::GetInstance()->SilenceAll();

		switch(CurrentDungeon)
		{
		case 0:
			CutsceneState::GetInstance()->RunCutscene(CutsceneState::SCENE_RESEARCHLABSTORY);
			break;
		case 1:
			CutsceneState::GetInstance()->RunCutscene(CutsceneState::SCENE_POWERSTORY);
			break;
		case 2:
			CutsceneState::GetInstance()->RunCutscene(CutsceneState::SCENE_MSTORY);
			break;
		case 3:
			CutsceneState::GetInstance()->RunCutscene(CutsceneState::SCENE_RSTORY);
			break;
		}
	}

	/*if(twoPlayer == false && player != nullptr)
	{
	om->RemoveObject(player);
	om->AddObject(player);
	}*/



	if(twoPlayer == true && player != nullptr && player2 != nullptr)
	{
		//om->RemoveObject(player);
		//om->RemoveObject(player2);

		player2->SetX(player->GetX() + 50);
		player2->SetY(player->GetY());

		//om->AddObject(player);
		//om->AddObject(player2);

		player1Wait = 0;
		player2Wait = 0;
	}

	// Decide which music to play
	if(newGame == true)
	{
		if(CSGD_XAudio2::GetInstance()->MusicIsSongPlaying(tutorialMusic) == false)
		{
			CSGD_XAudio2::GetInstance()->MusicStopSong(shipMusic);
			CSGD_XAudio2::GetInstance()->MusicPlaySong(tutorialMusic,true);
		}
	}
	else if(newGame == false)
	{
		switch(CurrentDungeon)
		{
		case 0:
			{
				if(CSGD_XAudio2::GetInstance()->MusicIsSongPlaying(researchMusic) == false)
				{
					CSGD_XAudio2::GetInstance()->MusicStopSong(shipMusic);
					CSGD_XAudio2::GetInstance()->MusicPlaySong(researchMusic,true);
				}
			}
			break;

		case 1:
			{
				if(CSGD_XAudio2::GetInstance()->MusicIsSongPlaying(powerMusic) == false)
				{
					CSGD_XAudio2::GetInstance()->MusicStopSong(shipMusic);
					CSGD_XAudio2::GetInstance()->MusicPlaySong(powerMusic,true);
				}
			}
			break;

		case 2:
			{
				if(CSGD_XAudio2::GetInstance()->MusicIsSongPlaying(militaryMusic) == false)
				{
					CSGD_XAudio2::GetInstance()->MusicStopSong(shipMusic);
					CSGD_XAudio2::GetInstance()->MusicPlaySong(militaryMusic,true);
				}
			}
			break;

		case 3:
			{
				if(CSGD_XAudio2::GetInstance()->MusicIsSongPlaying(recycleMusic) == false)
				{
					CSGD_XAudio2::GetInstance()->MusicStopSong(shipMusic);
					CSGD_XAudio2::GetInstance()->MusicPlaySong(recycleMusic,true);
				}
			}
			break;
		}
	}
}

void GamePlayState::HUDmagic()
{
	////////////////////
	//* FIRST PLAYER *//
	////////////////////

	// New Health Bar
	RECT healthHUD = {220,8,284,72}; //8,8,70,70
	RECT healthHUDinvert = {288,8,352,(LONG)(72 - (player->GetHealth()/player->GetMaxHealth())*64)};

	if(healthHUD.bottom >= 72)
		healthHUD.bottom = 72;

	if(healthHUDinvert.bottom >= 72)
		healthHUDinvert.bottom = 72;

	CSGD_TextureManager::GetInstance()->Draw(hudIconsID,32,27,.5f,.5f,&healthHUD/*,0,0,0,D3DCOLOR_ARGB(255,0,0,255)*/);
	CSGD_TextureManager::GetInstance()->Draw(hudIconsID,32,27,.5f,.5f,&healthHUDinvert/*,0,0,0,D3DCOLOR_ARGB(255,255,0,0)*/);

	// New Energy Bar
	RECT energyHUD = {108,76,204,140};
	RECT energyHUDinvert = {208,76,(LONG)(304 - (player->GetEnergy()/player->GetMaxEnergy())*96),140};
	if(energyHUD.right <= 108)
		energyHUD.right = 108;
	if(energyHUDinvert.right <= 208)
		energyHUDinvert.right = 208;
	CSGD_TextureManager::GetInstance()->Draw(hudIconsID,96,27,.5f,.5f,&energyHUD/*,0,0,0,D3DCOLOR_ARGB(255,55,90,190)*/);
	CSGD_TextureManager::GetInstance()->Draw(hudIconsID,96,27,.5f,.5f,&energyHUDinvert/*,0,0,0,D3DCOLOR_ARGB(255,200,45,45)*/);

	// Weapons
	if(player->GetCurWeapon() == 1)
	{
		switch(player->GetRangedWeaponType())
		{
		case RW_BASE:
			{
				RECT ranged = {208,280,304,344};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,225,20,.5f,.5f,&ranged);
			}
			break;

		case RW_ROCKET:
			{
				RECT ranged = {308,348,404,412};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,225,20,.5f,.5f,&ranged);
			}
			break;

		case RW_EMP:
			{
				RECT ranged = {208,348,304,412};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,225,20,.5f,.5f,&ranged);
			}
			break;

		case RW_FLAME:
			{
				RECT ranged = {308,280,404,344};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,225,20,.5f,.5f,&ranged);
			}
			break;
		}

		switch(player->GetMeleeWeaponType())
		{
		case MW_BASE:
			{
				// 208,144,304,208
				//RECT cooldown = {208,144,(LONG)(304 - (player->GetMelee()->GetCoolTimer()/player->GetMelee()->GetCoolSeconds())*96),208};
				//if(cooldown.right <= 208)
				//	cooldown.right = 208;
				RECT melee = {8,144,104,208};

				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,200,27,.5f,.5f,&melee);
				//CSGD_TextureManager::GetInstance()->Draw(hudIconsID,200,22,.5f,.5f,&cooldown);
			}
			break;

		case MW_SWORD:
			{
				// 208,212,304,276
				RECT cooldown = {208,212,(LONG)(304 - (player->GetMelee()->GetCoolTimer()/player->GetMelee()->GetCoolSeconds())*96),276};
				if(cooldown.right <= 208)
					cooldown.right = 208;
				RECT melee = {8,212,104,276};

				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,200,27,.5f,.5f,&melee);
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,200,27,.5f,.5f,&cooldown);
			}
			break;

		case MW_SAW:
			{
				// 308,212,404,276
				RECT cooldown = {308,212,(LONG)(404 - (player->GetMelee()->GetCoolTimer()/player->GetMelee()->GetCoolSeconds())*96),276};
				if(cooldown.right <= 308)
					cooldown.right = 308;
				RECT melee = {108,212,204,276};

				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,200,27,.5f,.5f,&melee);
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,200,27,.5f,.5f,&cooldown);
			}
			break;

		case MW_CLAW:
			{
				// 308,144,404,208
				RECT cooldown = {308,144,(LONG)(404 - (player->GetMelee()->GetCoolTimer()/player->GetMelee()->GetCoolSeconds())*96),208};
				if(cooldown.right <= 308)
					cooldown.right = 308;
				RECT melee = {108,144,204,208};

				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,200,27,.5f,.5f,&melee);
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,200,27,.5f,.5f,&cooldown);
			}
			break;
		}

	}
	else if(player->GetCurWeapon() == 2)
	{
		switch(player->GetMeleeWeaponType())
		{
		case MW_BASE:
			{
				RECT melee = {208,144,304,208};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,225,20,.5f,.5f,&melee);
			}
			break;

		case MW_SWORD:
			{
				RECT melee = {208,212,304,276};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,225,20,.5f,.5f,&melee);
			}
			break;

		case MW_SAW:
			{
				RECT melee = {308,212,404,276};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,225,20,.5f,.5f,&melee);
			}
			break;

		case MW_CLAW:
			{
				RECT melee = {308,144,404,208};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,225,20,.5f,.5f,&melee);
			}
			break;
		}

		switch(player->GetRangedWeaponType())
		{
		case RW_BASE:
			{
				// 8,280,104,344
				//RECT cooldown = {208,280,(LONG)(304 - (player->GetRanged()->GetCoolTimer()/player->GetRanged()->GetCoolSeconds())*96),344};
				//if(cooldown.right <= 208)
				//	cooldown.right = 208;
				RECT ranged = {8,280,104,344};

				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,200,27,.5f,.5f,&ranged);
				//CSGD_TextureManager::GetInstance()->Draw(hudIconsID,200,22,.5f,.5f,&cooldown);
			}
			break;

		case RW_ROCKET:
			{
				// 308,348,404,412
				RECT cooldown = {308,348,(LONG)(404 - (player->GetRanged()->GetCoolTimer()/player->GetRanged()->GetCoolSeconds())*96),412};
				if(cooldown.right <= 308)
					cooldown.right = 308;
				RECT ranged = {108,348,204,412};

				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,200,27,.5f,.5f,&ranged);
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,200,27,.5f,.5f,&cooldown);
			}
			break;

		case RW_EMP:
			{
				// 8,348,104,412
				RECT cooldown = {208,348,(LONG)(304 - (player->GetRanged()->GetCoolTimer()/player->GetRanged()->GetCoolSeconds())*96),412};
				if(cooldown.right <= 208)
					cooldown.right = 208;
				RECT ranged = {8,348,104,412};

				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,200,27,.5f,.5f,&ranged);
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,200,27,.5f,.5f,&cooldown);
			}
			break;

		case RW_FLAME:
			{
				// 308,280,404,344
				RECT cooldown = {308,280,(LONG)(404 - (player->GetRanged()->GetCoolTimer()/player->GetRanged()->GetCoolSeconds())*96),344};
				if(cooldown.right <= 308)
					cooldown.right = 308;
				RECT ranged = {108,280,204,344};

				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,200,27,.5f,.5f,&ranged);
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,200,27,.5f,.5f,&cooldown);
			}
			break;
		}
	}

	// Skills
	switch(player->GetSkillOne())
	{
	case CAMO:
		{
			if(player->GetCamo() == true)
			{
				RECT camo = {8,416,72,480};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,32,548,.5f,.5f,&camo);
			}
			else
			{
				RECT camo = {212,416,276,480};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,32,548,.5f,.5f,&camo);
			}
		}
		break;

	case CLOCK:
		{
			if(player->GetClock() == true)
			{
				RECT clock = {76,416,140,480};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,32,548,.5f,.5f,&clock);
			}
			else
			{
				RECT clock = {280,416,344,480};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,32,548,.5f,.5f,&clock);
			}
		}
		break;

	case SHIELD:
		{
			if(player->GetShield() == true)
			{
				RECT shield = {144,416,208,480};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,32,548,.5f,.5f,&shield);
			}
			else
			{
				RECT shield = {348,416,412,480};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,32,548,.5f,.5f,&shield);
			}	
		}
		break;
	}

	switch(player->GetSkillTwo())
	{
	case CAMO:
		{
			if(player->GetCamo() == true)
			{
				RECT camo = {8,416,72,480};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,100,548,.5f,.5f,&camo);
			}
			else
			{
				RECT camo = {212,416,276,480};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,100,548,.5f,.5f,&camo);
			}
		}
		break;

	case CLOCK:
		{
			if(player->GetClock() == true)
			{
				RECT clock = {76,416,140,480};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,100,548,.5f,.5f,&clock);
			}
			else
			{
				RECT clock = {280,416,344,480};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,100,548,.5f,.5f,&clock);
			}
		}
		break;

	case SHIELD:
		{
			if(player->GetShield() == true)
			{
				RECT shield = {144,416,208,480};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,100,548,.5f,.5f,&shield);
			}
			else
			{
				RECT shield = {348,416,412,480};
				CSGD_TextureManager::GetInstance()->Draw(hudIconsID,100,548,.5f,.5f,&shield);
			}
		}
		break;
	}


	/////////////////////
	//* SECOND PLAYER *//
	/////////////////////

	if(twoPlayer == true)
	{	
		// New Health Bar
		RECT healthHUD = {220,8,284,72}; //8,8,70,70
		RECT healthHUDinvert = {288,8,352,(LONG)(72 - (player2->GetHealth()/player2->GetMaxHealth())*64)};

		if(healthHUDinvert.bottom >= 72)
			healthHUDinvert.bottom = 72;

		CSGD_TextureManager::GetInstance()->Draw(hudIconsID,532,27,.5f,.5f,&healthHUD);
		CSGD_TextureManager::GetInstance()->Draw(hudIconsID,532,27,.5f,.5f,&healthHUDinvert);

		// New Energy Bar
		RECT energyHUD = {108,76,204,140};
		RECT energyHUDinvert = {208,76,(LONG)(304 - (player2->GetEnergy()/player2->GetMaxEnergy())*96),140};
		if(energyHUDinvert.right <= 208)
			energyHUDinvert.right = 208;
		CSGD_TextureManager::GetInstance()->Draw(hudIconsID,596,27,.5f,.5f,&energyHUD);
		CSGD_TextureManager::GetInstance()->Draw(hudIconsID,596,27,.5f,.5f,&energyHUDinvert);

		// Weapons
		if(player2->GetCurWeapon() == 1)
		{
			switch(player2->GetRangedWeaponType())
			{
			case RW_BASE:
				{
					RECT ranged = {208,280,304,344};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,720,20,.5f,.5f,&ranged);
				}
				break;

			case RW_ROCKET:
				{
					RECT ranged = {308,348,404,412};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,720,20,.5f,.5f,&ranged);
				}
				break;

			case RW_EMP:
				{
					RECT ranged = {208,348,304,412};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,720,20,.5f,.5f,&ranged);
				}
				break;

			case RW_FLAME:
				{
					RECT ranged = {308,280,404,344};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,720,20,.5f,.5f,&ranged);
				}
				break;
			}

			switch(player2->GetMeleeWeaponType())
			{
			case MW_BASE:
				{
					// 208,144,304,208
					//RECT cooldown = {208,144,(LONG)(304 - (player2->GetMelee()->GetCoolTimer()/player2->GetMelee()->GetCoolSeconds())*96),208};
					//if(cooldown.right <= 208)
					//	cooldown.right = 208;
					RECT melee = {8,144,104,208};

					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,695,27,.5f,.5f,&melee);
					//CSGD_TextureManager::GetInstance()->Draw(hudIconsID,200,22,.5f,.5f,&cooldown);
				}
				break;

			case MW_SWORD:
				{
					// 208,212,304,276
					RECT cooldown = {208,212,(LONG)(304 - (player2->GetMelee()->GetCoolTimer()/player2->GetMelee()->GetCoolSeconds())*96),276};
					if(cooldown.right <= 208)
						cooldown.right = 208;
					RECT melee = {8,212,104,276};

					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,695,27,.5f,.5f,&melee);
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,695,27,.5f,.5f,&cooldown);
				}
				break;

			case MW_SAW:
				{
					// 308,212,404,276
					RECT cooldown = {308,212,(LONG)(404 - (player2->GetMelee()->GetCoolTimer()/player2->GetMelee()->GetCoolSeconds())*96),276};
					if(cooldown.right <= 308)
						cooldown.right = 308;
					RECT melee = {108,212,204,276};

					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,695,27,.5f,.5f,&melee);
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,695,27,.5f,.5f,&cooldown);
				}
				break;

			case MW_CLAW:
				{
					// 308,144,404,208
					RECT cooldown = {308,144,(LONG)(404 - (player2->GetMelee()->GetCoolTimer()/player2->GetMelee()->GetCoolSeconds())*96),208};
					if(cooldown.right <= 308)
						cooldown.right = 308;
					RECT melee = {108,144,204,208};

					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,695,27,.5f,.5f,&melee);
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,695,27,.5f,.5f,&cooldown);
				}
				break;
			}

		}
		else if(player2->GetCurWeapon() == 2)
		{
			switch(player2->GetMeleeWeaponType())
			{
			case MW_BASE:
				{
					RECT melee = {208,144,304,208};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,720,20,.5f,.5f,&melee);
				}
				break;

			case MW_SWORD:
				{
					RECT melee = {208,212,304,276};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,720,20,.5f,.5f,&melee);
				}
				break;

			case MW_SAW:
				{
					RECT melee = {308,212,404,276};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,720,20,.5f,.5f,&melee);
				}
				break;

			case MW_CLAW:
				{
					RECT melee = {308,144,404,208};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,720,20,.5f,.5f,&melee);
				}
				break;
			}

			switch(player2->GetRangedWeaponType())
			{
			case RW_BASE:
				{
					// 8,280,104,344
					//RECT cooldown = {208,280,(LONG)(304 - (player2->GetRanged()->GetCoolTimer()/player2->GetRanged()->GetCoolSeconds())*96),344};
					//if(cooldown.right <= 208)
					//	cooldown.right = 208;
					RECT ranged = {8,280,104,344};

					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,695,27,.5f,.5f,&ranged);
					//CSGD_TextureManager::GetInstance()->Draw(hudIconsID,200,22,.5f,.5f,&cooldown);
				}
				break;

			case RW_ROCKET:
				{
					// 308,348,404,412
					RECT cooldown = {308,348,(LONG)(404 - (player2->GetRanged()->GetCoolTimer()/player2->GetRanged()->GetCoolSeconds())*96),412};
					if(cooldown.right <= 308)
						cooldown.right = 308;
					RECT ranged = {108,348,204,412};

					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,695,27,.5f,.5f,&ranged);
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,695,27,.5f,.5f,&cooldown);
				}
				break;

			case RW_EMP:
				{
					// 8,348,104,412
					RECT cooldown = {208,348,(LONG)(304 - (player2->GetRanged()->GetCoolTimer()/player2->GetRanged()->GetCoolSeconds())*96),412};
					if(cooldown.right <= 208)
						cooldown.right = 208;
					RECT ranged = {8,348,104,412};

					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,695,27,.5f,.5f,&ranged);
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,695,27,.5f,.5f,&cooldown);
				}
				break;

			case RW_FLAME:
				{
					// 308,280,404,344
					RECT cooldown = {308,280,(LONG)(404 - (player2->GetRanged()->GetCoolTimer()/player2->GetRanged()->GetCoolSeconds())*96),344};
					if(cooldown.right <= 308)
						cooldown.right = 308;
					RECT ranged = {108,280,204,344};

					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,695,27,.5f,.5f,&ranged);
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,695,27,.5f,.5f,&cooldown);
				}
				break;
			}
		}

		// Skills
		switch(player2->GetSkillOne())
		{
		case CAMO:
			{
				if(player2->GetCamo() == true)
				{
					RECT camo = {8,416,72,480};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,668,548,.5f,.5f,&camo);
				}
				else
				{
					RECT camo = {212,416,276,480};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,668,548,.5f,.5f,&camo);
				}
			}
			break;

		case CLOCK:
			{
				if(player2->GetClock() == true)
				{
					RECT clock = {76,416,140,480};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,668,548,.5f,.5f,&clock);
				}
				else
				{
					RECT clock = {280,416,344,480};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,668,548,.5f,.5f,&clock);
				}
			}
			break;

		case SHIELD:
			{
				if(player2->GetShield() == true)
				{
					RECT shield = {144,416,208,480};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,668,548,.5f,.5f,&shield);
				}
				else
				{
					RECT shield = {348,416,412,480};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,668,548,.5f,.5f,&shield);
				}	
			}
			break;
		}

		switch(player2->GetSkillTwo())
		{
		case CAMO:
			{
				if(player2->GetCamo() == true)
				{
					RECT camo = {8,416,72,480};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,736,548,.5f,.5f,&camo);
				}
				else
				{
					RECT camo = {212,416,276,480};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,736,548,.5f,.5f,&camo);
				}
			}
			break;

		case CLOCK:
			{
				if(player2->GetClock() == true)
				{
					RECT clock = {76,416,140,480};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,736,548,.5f,.5f,&clock);
				}
				else
				{
					RECT clock = {280,416,344,480};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,736,548,.5f,.5f,&clock);
				}
			}
			break;

		case SHIELD:
			{
				if(player2->GetShield() == true)
				{
					RECT shield = {144,416,208,480};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,736,548,.5f,.5f,&shield);
				}
				else
				{
					RECT shield = {348,416,412,480};
					CSGD_TextureManager::GetInstance()->Draw(hudIconsID,736,548,.5f,.5f,&shield);
				}
			}
			break;
		}
	}

}

// Add a chest
void GamePlayState::AddChest(Chest* newChest)
{
	chestList.push_back(newChest);
	om->AddObject(newChest);
	newChest->Release();
}

// Add a level hazard
void GamePlayState::AddHazard(Hazard* hazard)
{
	LevelHazards.push_back(hazard);
	om->AddObject(hazard);
	hazard->Release();
}